#include "Event.h"

Event::Event() {
    entryNo = 0;
    this->dateTime = (tm*)malloc(sizeof(tm));
//    time_t rawtime;
//    time(&rawtime);
//    this->dateTime = localtime(&rawtime);
   
    position.SetAbsis(0);
    position.SetOrdinat(0);
    description = "description";
    participantCount = 0;

}

Event::Event(long entryNo, struct tm* times, Point<float> position, string description, vector<string> participants, short int participantCount) {
    this->entryNo = entryNo;
    this->dateTime = times;
    this->position = position;
    this->description = description;
    this->participantCount = participantCount;
    this->participants = participants;
}

Event::Event(const Event& _event) {
    this->entryNo = _event.entryNo;
    this->description = _event.description;
    this->dateTime = _event.dateTime;
    this->position = _event.position;
    this->participantCount = _event.participantCount;
    this->participants = _event.participants;
}

Event::~Event() {

}

Event& Event::operator =(const Event& _event) {
    this->entryNo = _event.entryNo;
    this->dateTime = _event.dateTime;
    this->position = _event.position;
    this->participantCount = _event.participantCount;
    this->participants = _event.participants;
    this->description = _event.description;
    return *this;
}

bool Event::operator<(const Event& _event) {
    return this->dateTime < _event.GetDateTime();
}

bool Event::operator ==(const Event& _event) {
    return this->dateTime == _event.GetDateTime();
}

void Event::printTime() {
    char buffer[80];
    //struct tm* timeinfo = localtime(&dateTime);
    strftime(buffer, 80, "%x.", dateTime);
    cout << buffer << endl;

}

string Event::toString() {
    string ret_val = "";
    ostringstream oss;
    oss << entryNo;
    ret_val += "#"+oss.str() + "\n";
    description.erase(description.find_last_not_of(" \n\r\t")+1);
    ret_val += description+"\n";
    //free(temp);
    char* temp;
    temp = (char*) malloc(100 * sizeof (char));
    sprintf(temp, "%.4f,%.4f \n", position.GetAbsis(), position.GetOrdinat());
    ret_val.append(temp);
    strftime(temp, 80, "%Y-%m-%d@%H:%M:%S", dateTime);
    ret_val.append(temp);
    ret_val.append("\n");
    free(temp);
    int i;
    for (i = 0; participants.size() != 0 && i < participants.size() - 1; i++) {
        string s = participants[i];
        s.erase(s.find_last_not_of(" \n\r\t")+1);
        ret_val += participants[i] + ",";
    }
    if (i != 0) {
        string s = participants[i];
        s.erase(s.find_last_not_of(" \n\r\t")+1);
        ret_val += s + "\n";
    }
    return ret_val;
}




